<cfparam default="none" name="url.api" />
            

<!--- GroupSearch --->
<cfif url.api eq "GroupSearch">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="GET">
            <cfparam default="" name="url.groupSearchString" />
            <cfscript>
                Groups = createObject("component", "#session.rootDir#.objects.Groups").init();
                GetGroupByDN = Groups.getGroupByDN(url.groupSearchString);
                writeOutput(SerializeJSON(GetGroupByDN));
            </cfscript>
        </cfcase>
    </cfswitch>
</cfif>
<!--- GroupSearch --->


<!--- UserSearch --->
<cfif url.api eq "UserSearch">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="GET">
            <cfparam default="" name="url.groupSearchString" />
            <cfscript>
                Users = createObject("component", "#session.rootDir#.objects.Users").init();
                GetUserBYDN = Users.getADUserByDN(url.userSearchString);
                writeOutput(SerializeJSON(GetUserBYDN));
            </cfscript>
        </cfcase>
    </cfswitch>
</cfif>
<!--- UserSearch --->


<!--- Remove User From Group --->
<cfif url.api eq "RemoveUserFromGroup">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST"><!---SHOULD BE DELETE--->
            <cfscript>
                import models.*;
                ChangeLog = entityNew("ChangeLog");
                Groups = createObject("component", '#session.rootDir#.objects.Groups').init();
                Rosetta = entityLoadByPK("RosettaStone", FORM.RosettaID);
                UserAccount = Rosetta.getUserAccount();
                UserDN = Groups.SCFAD.getUserDNByAccountName(UserAccount);
                GroupName = Groups.SCFAD.getGroupNameByDN(FORM.GroupDN);
                Groups.removeUserFromGroup(FORM.GroupDN,UserDN);
                ChangeLog.addChangeLog("#UserDN#", "", 'Remove User from Group: #FORM.GroupDN#', "Groups");
                //writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfcase>
        <cfdefaultcase>
            <cfscript>
               writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfdefaultcase>
    </cfswitch>
</cfif>
<!--- Remove User From Group --->


<!--- Save Description --->
<cfif url.api eq "UpdateGroupDescription">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST"><!---SHOULD BE PUT--->
            <cfscript>
                import models.*;
                ChangeLog = entityNew("ChangeLog");
                Groups = createObject("component", '#session.rootDir#.objects.Groups').init();
                PreviousValue = Groups.SCFAD.getUserAttributesByDN( FORM.GroupDN, 'description').description;
                Groups.SCFAD.replaceAttributesByDN( FORM.GroupDN, "description=#Trim(FORM.GroupDescription)#" );
                ChangeLog.addChangeLog("#PreviousValue#", "#Trim(FORM.GroupDescription)#", 'Update description for group: #FORM.GroupDN#', "Groups");
                //writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfcase>
        <cfdefaultcase>
            <cfscript>
               writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfdefaultcase>
    </cfswitch>
</cfif>
<!--- Save Description --->



<!--- Add User as Designee --->
<cfif url.api eq "RemoveDesignee">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST">
            <cfscript>
                import models.*;
                ChangeLog = entityNew("ChangeLog");               
                Designee = entityloadbypk("Designees", FORM.DesigneeID);                
                ChangeLog.addChangeLog("UserDN = #Designee.getADUserDN#, GroupDN = #Designee.getADGroup#", "UserDN = , GroupDN = ", 
                    'Remove designee from group: #FORM.GroupDN#', "Desginee");
                EntityDelete(Designee);
            </cfscript>
        </cfcase>
        <cfdefaultcase>
            <cfscript>
               writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfdefaultcase>
    </cfswitch>
</cfif>
<!--- Add User as Designee --->



<!--- Add User as Designee --->
<cfif url.api eq "AddDesignee">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST">
            <cfscript>
                import models.*;
                ChangeLog = entityNew("ChangeLog");
                Groups = createObject("component", '#session.rootDir#.objects.Groups').init();
                Users = createObject("component", '#session.rootDir#.objects.Users').init();
                UserAccount = Users.SCFAD.getUserAttributesByDN(FORM.UserDN).sAMAccountName;
                Rosetta = entityLoad("RosettaStone", {UserAccount = '#UserAccount#' } );
                Designee = entityNew("Designees");
                Designee.setADGroup(FORM.GroupDN);
                Designee.setRosettaID(Rosetta[1].getRosettaID());
                Designee.setADUserAccount(UserAccount);
                Designee.setADUserDN(FORM.UserDN);
                EntitySave(Designee, true);               
                ChangeLog.addChangeLog("", "#FORM.UserDN#", 'Add as group designee: #FORM.GroupDN#', "Desginee");
                //writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfcase>
        <cfdefaultcase>
            <cfscript>
               writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfdefaultcase>
    </cfswitch>
</cfif>
<!--- Add User as Designee --->


<!--- Add User to Group --->
<cfif url.api eq "AddUserToGroup">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST"><!---SHOULD BE PUT--->
            <cfscript>
                import models.*;
                ChangeLog = entityNew("ChangeLog");
                Groups = createObject("component", '#session.rootDir#.objects.Groups').init();
                Users = createObject("component", '#session.rootDir#.objects.Users').init();
                UserAccount = Users.SCFAD.getUserAttributesByDN(FORM.UserDN).sAMAccountName;
                Rosetta = entityLoad("RosettaStone", {UserAccount = '#UserAccount#' } );
                GroupName = Groups.SCFAD.getGroupNameByDN(FORM.GroupDN);
                Groups.SCFAD.addUserToGroup( FORM.UserDN, FORM.GroupDN  );
                ChangeLog.addChangeLog("", "#FORM.UserDN#", 'Add user to group: #FORM.GroupDN#', "Users");
                //writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfcase>
        <cfdefaultcase>
            <cfscript>
               writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfdefaultcase>
    </cfswitch>
</cfif>
<!--- Add User to Group --->


<!--- Add User as guest to group --->
<cfif url.api eq "AddGuestToGroup">
    <cfswitch expression="#CGI.REQUEST_METHOD#">
        <cfcase value="POST">
            <cfscript>
                writeDump(FORM);
               /* import models.*;
                ChangeLog = entityNew("ChangeLog");
                Groups = createObject("component", '#session.rootDir#.objects.Groups').init();
                Users = createObject("component", '#session.rootDir#.objects.Users').init();
                UserAccount = Users.SCFAD.getUserAttributesByDN(FORM.UserDN).sAMAccountName;
                Rosetta = entityLoad("RosettaStone", {UserAccount = '#UserAccount#' } );
                Designee = entityNew("Designees");
                Designee.setADGroup(FORM.GroupDN);
                Designee.setRosettaID(Rosetta[1].getRosettaID());
                Designee.setADUserAccount(UserAccount);
                Designee.setADUserDN(FORM.UserDN);
                EntitySave(Designee, true);  */             
                //ChangeLog.addChangeLog("", "#FORM.UserDN#", 'Add as group designee: #FORM.GroupDN#', "Desginee");
            </cfscript>
        </cfcase>
        <cfdefaultcase>
            <cfscript>
               writeOutput('#CGI.HTTP_REFERER#');
            </cfscript>
        </cfdefaultcase>
    </cfswitch>
</cfif>
<!--- Add User as guest to group --->